# coding:utf-8
import urllib.parse
import datetime
from urllib.parse import urljoin
from urllib.parse import urlparse
from urllib.parse import urlunparse
from posixpath import normpath
import hashlib
import codecs
import os
import json


class Urlchuli():
    """Url处理类，需要传入两个实参：Urlchuli('实参','编码类型') 默认utf-8
    url编码方法：url_bm() url解码方法：url_jm()"""

    def __init__(self,can,mazhi='utf-8'):
        self.can = can
        self.mazhi = mazhi

    def url_bm(self):
        """url_bm() 将传入的中文实参转为Urlencode编码"""
        quma = str(self.can).encode(self.mazhi)
        bianma = urllib.parse.quote(quma)
        return bianma

    def url_jm(self):
        """url_jm() 将传入的url进行解码成中文"""
        quma = str(self.can)
        jiema = urllib.parse.unquote(quma,self.mazhi)
        return jiema


class Quriqi():
    """取区间日期模块，主要传入两个参数:
    Quriqi('20180601','20180608').suanriqi()  将会返回list类型 
    此类用的模块为datetime
    """
    def __init__(self,qishi,jiezhi):
        self.start = qishi
        self.end = jiezhi
        self.suanriqi()

    def suanriqi(self):
        datestart = datetime.datetime.strptime(self.start,'%Y%m%d')  
        dateend = datetime.datetime.strptime(self.end,'%Y%m%d')  
        riqi_list = [] 
        while datestart < dateend:
            datestart += datetime.timedelta(days=1)
            qu = datestart.strftime('%Y%m%d')
            riqi_list.append(qu)
        return riqi_list


class Urlhecheng():
    """url合成链接
    Urlhecheng('当前url','截取到的url').pingjie()
    """
    def __init__(self,base,url):
        self.base = base
        self.url = url
        self.pingjie()

    def pingjie(self):
        url1 = urljoin(self.base, self.url)
        arr = urlparse(url1)
        path = normpath(arr[2])
        return urlunparse((arr.scheme, arr.netloc, path, arr.params, arr.query, arr.fragment))


class Pmd5():
    """用于MD5压缩，ysmd5()为一层压缩，jysmd5()为二层压缩。"""
    def __init__(self,shuju):
        # 传入数据
        self.shujuy = shuju

    def ys_md5(self):
        """转换成字符串UTF8格式来压缩MD5；格式：ysmd5(实参)，自动将其他类型转换为str类型。"""
        ysz = hashlib.md5(str(self.shujuy).encode(encoding='UTF-8')).hexdigest()
        return ysz

    def j_ys_md5(self):
        """将实参转换为字符串UTF8格式并压缩MD5，然后在此基础上继续压缩一次MD5。"""
        ysz_b = self.ys_md5()
        ysz_a = hashlib.md5(str(ysz_b).encode(encoding='UTF-8')).hexdigest()
        return ysz_a


class Quchong():
    """url去重复
    Quchong(url,对比的json相对路径).urlquchong()
    返回bool类型：如果不存在文本中返回True，并添加该MD5到文本中
    如果存在于文本中返回False。
    """
    def __init__(self,url,route):
        self.url = url
        self.route = route
        
    def urlquchong(self):
        yesno = False
        yazhi = Pmd5(self.url).ys_md5()
        route_url = os.path.abspath(self.route)
        with codecs.open(route_url,'r','utf-8') as open_urlmd5:
            for if_md5 in open_urlmd5:
                if yazhi not in if_md5:
                    yesno = True
                    with codecs.open(route_url,'a','utf-8') as xie:
                        json.dump(yazhi,xie,ensure_ascii=False)
                
        return yesno
